package org.dftproject.gedcom.core;

import org.dftproject.genesis.data.genealogy.INote;

public class Note implements INote {

	private IGedcom gedcom;
	private String id;
	private Line record;
	
	public Note(Line record) {
		if (record == null)
			throw new NullPointerException("record must not be null");
		this.record = record;
	}

	public Note(IGedcom gedcom, String id) {
		if (gedcom == null || id == null || "".equals(id))
			throw new NullPointerException("gedcom and id must not be null or empty");
		this.gedcom = gedcom;
		this.id = id;
	}

	public String getContents() {
		if (record == null) {
			record = gedcom.getRecord(id);
			if (record == null)
				return null;
		}
		
		String line = record.getValue();
		StringBuilder sb = new StringBuilder(line == null ? "" : line);
		
		for (Line child : record.getChildren(new String[] { "CONC", "CONT" })) {
			if ("CONT".equals(child.getTag()))
				sb.append("\n");
			line = child.getValue();
			sb.append(line == null ? "" : line);
		}
		
		return sb.toString().replaceAll("^\\s+|\\s+$", "");
	}

}
